﻿using System;
using BitButterCORE.Event;
using BitButterCORE.Factory;
using BitButterCORE.Object;

namespace BitButterCORE.Rule
{
	public abstract class InterruptibleRule<TRule> : BaseRule<TRule>
		where TRule : BaseRule<TRule>
	{
		protected override void Apply(IObjectReference<BaseObject> sender, BaseEventArgs eventArgs)
		{
			if (TokenFactory.Instance.HasObject)
			{
				if (RuleIsAppliedToObject(sender))
				{
					var (matchedMethod, parameters) = GetApplierMethodAndParameters(sender, eventArgs);
					TokenFactory.Instance.InterruptApplierMethod(new InterruptedApplierMethod(matchedMethod, this, parameters));
				}
			}
			else
			{
				base.Apply(sender, eventArgs);
			}
		}
	}
}
